package com.mojo.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 学生类-演示Bean的生命周期流程
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/9/19 15:33
 */

public class Student
        implements BeanFactoryAware, BeanNameAware, ApplicationContextAware,
        InitializingBean, DisposableBean {
    private String username;
    private int age;
    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;
    private String beanName;

    public Student() {
        System.out.println("创建Student实例");
    }

    public void setUsername(String username) {
        System.out.println("设置username");
        this.username = username;
    }

    public void setAge(int age) {
        System.out.println("设置age");
        this.age = age;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

        System.out.println("设置beanFactory");
        this.beanFactory = beanFactory;

    }

    @Override
    public void setBeanName(String name) {
        System.out.println("设置beanName");
        this.beanName = name;

    }

    @Override
    public void destroy() throws Exception {
        System.out.println("销毁student实例");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("属性设置之后操作。。。");

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("设置applicationContext");
        this.applicationContext = applicationContext;
    }

    public void doInit() {
        System.out.println("student初始化");
    }

    public void doDestroy() {
        System.out.println("student销毁");
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.mojo.pojo");
        Student student = context.getBean("student", Student.class);
        context.registerShutdownHook();
    }
}
